faApp.controller('CompanyListCtrl',['$scope','companyService','$location','$controller','$route',function($scope,companyService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.companys = [];
  $scope.loading = true;

	$scope.$watch('config',function(){
    companyService.companyList($scope.config).then(function(json){
      $scope.companys = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true)

	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1,
		"cName" : '',
	};

  $scope.searchMap = {
		"cName" : '', 
  };

  $scope.search = function(){

    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });

  };
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	}; 

  $scope.deleteCompany = function(id){
    companyService.companyDelete(id).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("删除成功");
        $route.reload();
      }
    });
  };
}]);

faApp.controller('CompanyAddCtrl',['$scope','companyService','$location','$controller','$timeout',function($scope,companyService,$location,$controller,$timeout) {   
	$controller('BaseListCtrl', {$scope: $scope}); 

  $scope.company = {};
  $scope.loading = true; 

	$scope.save = function(){
    if ($scope.form.$invalid){
      return;
    }else{
      companyService.companyAdd($scope.company).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/company/list");
        }
      });
    }
  };

  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.company.cShortImageUri = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  }); 
}]);

faApp.controller('CompanyEditCtrl',['$scope','companyService','$location','$controller','$routeParams','$filter',function($scope,companyService,$location,$controller,$routeParams,$filter) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.company = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;

  companyService.companyQuery($scope.id).then(function(json){
    $scope.company =  json.dto;
  });

	$scope.save = function(){
    if ($scope.form.$invalid){
      return;
    }else{
      companyService.companyEdit($scope.company).then(function(json){
        if (json.statusCode){
          alert(json.statusCode);
        }else{
          $location.path("/company/list");
        }
      });
    }
  };


  $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.company.cShortImageUri = App.staticHost + response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  }); 
}]);


faApp.controller('CompanyViewCtrl',['$scope','companyService','$location','$controller','$routeParams','$filter',function($scope,companyService,$location,$controller,$routeParams,$filter) {   
	$controller('BaseCtrl', {$scope: $scope}); 
  $scope.id = $routeParams.id;

  companyService.companyQuery($scope.id).then(function(json){
    $scope.company = json.dto;
  });

}]);
 
faApp.controller('CompanySubCtrl',['$scope','companyService','$location','$controller','$route',function($scope,companyService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.companys = [];
  $scope.loading = true;

	$scope.$watch('config',function(){
    companyService.subList($scope.config).then(function(json){
      $scope.companys = json.list;
      $scope.loading = false;
    });
  },true)

	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1,
		"subType" : '01',
	};

  $scope.searchMap = {
		"subType" : '', 
		"subMail" : '', 
  };

  $scope.search = function(){

    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });

  };
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	}; 

  $scope.deleteCompany = function(id){
    companyService.companyDelete(id).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $scope.alertNextPage("删除成功");
        $route.reload();
      }
    });
  };
}]);

